package com.zeng.jiudian.servlet.newType;

import com.alibaba.fastjson.JSONObject;
import com.zeng.jiudian.bean.NewTypeModel;
import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.service.NewTypeService;
import com.zeng.jiudian.service.impl.NewTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/8/13 - 14:09
 */
@WebServlet("/back/newTypeList")
public class NewTypeListServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String typeName = req.getParameter("typeName");
        String isShow = req.getParameter("isShow");
        String recommend = req.getParameter("recommend");
        String order = req.getParameter("order");
        String lift = req.getParameter("lift");
        String pageSize = req.getParameter("pageSize");
        String pageNum = req.getParameter("pageNum");
        String nowNum = req.getParameter("nowNum");

        NewTypeModel model = new NewTypeModel();
        model.setTypeName(typeName);
        model.setIsShow(isShow == null ? null : Integer.parseInt(isShow));
        model.setRecommend(recommend == null ? null : Integer.parseInt(recommend));
        model.setOrder(order);
        model.setLift(lift);

        PageModel pageModel = new PageModel();
        pageModel.setPageNum((pageNum == null || "".equals(pageNum) || "0".equals(pageNum)) ? 1 : Integer.parseInt(pageNum));
        pageModel.setPageSize((pageSize == null || "".equals(pageSize) || "0".equals(pageSize)) ? 10 : Integer.parseInt(pageSize));
        pageModel.setNowNum((nowNum == null || "".equals(nowNum) || "0".equals(nowNum)) ? 1 : Integer.parseInt(nowNum));
        model.setPageModel(pageModel);

        NewTypeService service = new NewTypeServiceImpl();
        List<NewTypeModel> list = service.findAll(model);
        int count = service.getCount(model);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
